package com.tairanchina.wiki.api;

/**
 * Created by 周靖捷 on 2016/10/4.
 */
public class TextBuilder {

    StringBuffer stringBuffer = new StringBuffer();


    public TextBuilder writeln(String text) {
        if (stringBuffer.length() > 0) {
            stringBuffer.append(System.lineSeparator());
        }
        if (text != null)
            stringBuffer.append(text);
        return this;
    }

    public TextBuilder write(String text) {
        if (text != null)
            stringBuffer.append(text);
        return this;
    }

    public TextBuilder nextLine() {
        stringBuffer.append(System.lineSeparator());
        return this;
    }

    public TextBuilder indent() {
        stringBuffer.append("\t");
        return this;
    }

    public TextBuilder indent(String multilineText) {
        return indent(multilineText, 1);
    }

    public TextBuilder indent(String multilineText, int tabNum) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < tabNum; i++) {
            stringBuffer.append("\t");
        }
        multilineText = multilineText.replaceAll("\\n", "\n"+stringBuffer.toString());
        this.stringBuffer.append(stringBuffer.toString());
        this.stringBuffer.append(multilineText);
        return this;
    }

    public String build() {
        return stringBuffer.toString();
    }

}
